const jwt = require("jsonwebtoken");
/**
 * Token的生成与校验
 */

//加密:登录
let secret = "王祖贤";
function create(data, expiresIn = 60 * 60 * 24 * 7) {
  //失效时间：7天
  let token = jwt.sign({ data }, secret, {
    expiresIn,
  });
  return token;
}

// let token = create(123456);
// console.log(token);

//解密:路由守卫
function verify(token) {
  let res;
  try {
    let result = jwt.verify(token, secret);
    console.log("token校验：", result);
    res = true;
  } catch (err) {
    res = false;
  }
  return res;
}

// let ok = verify(
//   "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoxMjM0NTYsImlhdCI6MTYwMDc1NzMxOSwiZXhwIjoxNjAwNzU3MzM5fQ.11DOxEVTJ5lh6tOAoK-TCv5kTq6JF5KLHWLkCZGLFlE"
// );
// console.log(ok);

module.exports = {
  create,
  verify,
};
